package ruxi.tema1;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Forecast extends ListActivity {

	static ForecastAdapter adapter;	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forecast);
		
		Intent intent = getIntent();
		String mycity = intent.getStringExtra("mycity");
		String myXML = intent.getStringExtra("myXML");
		
		
		adapter = new ForecastAdapter(this);
		setListAdapter (adapter);
		 try {
			constructForecast(myXML);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		this.setTitle("Weather in " + mycity);

		adapter.notifyDataSetChanged();
		
		if(adapter.getCount() > 0) {
			TextView text = (TextView) findViewById(R.id.text);
			text.setVisibility(View.INVISIBLE);
		}
	}



  
	void constructForecast(String myxml) throws Exception {
	
		Weather today = new Weather();
		InputStream is =  new ByteArrayInputStream (myxml.getBytes());
	
		//Incarcam XML-ul intr-un input stream
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(is);
		doc.getDocumentElement().normalize();
		
		NodeList nodeList = doc.getElementsByTagName("problem_cause");
		if(nodeList.getLength() > 0)
			return;
		
			
		
		
		
		//iau data din forecast_information
		nodeList = doc.getElementsByTagName("forecast_information");
		Node node = nodeList.item(0);
		Element forecastElem = (Element)node;
		NodeList forecastElements = forecastElem.getElementsByTagName("forecast_date");
		Node date = forecastElements.item(0);
		Element dateElement = (Element)date;
		today.isToday = true;
		today.date = dateElement.getAttribute("data");
		
		//iau datele curente
		nodeList = doc.getElementsByTagName("current_conditions");
		node = nodeList.item(0);
		Element crtElem = (Element)node;
		//iau conditiile
		NodeList crtElements = crtElem.getElementsByTagName("condition");
		Node data = crtElements.item(0);
		today.condition = ((Element) data).getAttribute("data");
		//iau temperatura
		crtElements = crtElem.getElementsByTagName("temp_c");
		data = crtElements.item(0);
		today.temp = ((Element) data).getAttribute("data");
		//iau umiditate
		
		crtElements = crtElem.getElementsByTagName("humidity");
		data = crtElements.item(0);
		today.humidity = ((Element) data).getAttribute("data");
		//iau temperatura
		crtElements = crtElem.getElementsByTagName("wind_condition");
		data = crtElements.item(0);
		today.windCond = ((Element) data).getAttribute("data");
		//iau iconita
		crtElements = crtElem.getElementsByTagName("icon");
		data = crtElements.item(0);
		today.icon = ((Element) data).getAttribute("data");
		today.icon = today.icon.substring(19, today.icon.length() - 4);
		
		adapter.prognoze.add(today);
		
		
		//iau datele curente
		NodeList forecastList = doc.getElementsByTagName("forecast_conditions");
		for (int i = 0; i < forecastList.getLength(); i++) {
			Weather day = new Weather();
			day.isToday = false;
			node = forecastList.item(i);
			crtElem = (Element)node;
			
			//iau ziua
			crtElements = crtElem.getElementsByTagName("day_of_week");
			data = crtElements.item(0);
			day.day = ((Element) data).getAttribute("data");
			
			//iau temp min
			crtElements = crtElem.getElementsByTagName("low");
			data = crtElements.item(0);
			day.lowT = (Integer.parseInt(((Element) data).getAttribute("data")) - 32) * 5 / 9;
		
			//iau temp max
			crtElements = crtElem.getElementsByTagName("high");
			data = crtElements.item(0);
			day.highT = (Integer.parseInt(((Element) data).getAttribute("data")) - 32) * 5 / 9;
			
			//iau conditiile meteo
			crtElements = crtElem.getElementsByTagName("condition");
			data = crtElements.item(0);
			day.condition = ((Element) data).getAttribute("data");
			
			//iau iconita
			crtElements = crtElem.getElementsByTagName("icon");
			data = crtElements.item(0);
			day.icon = ((Element) data).getAttribute("data");
			day.icon = day.icon.substring(19, day.icon.length() - 4);
			adapter.prognoze.add(day);
		}
	
	 
	

	}




	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent myintent = new Intent(Forecast.this, Tema1.class);
		startActivity(myintent);
		
	}


}

